package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MenuDao;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MenuService.class)
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    public List<Map> findByUsername(String username) {

        List<Menu> menus = menuDao.findMenuByUsername(username);

        List<Map> result = new ArrayList<>();
        // 遍历菜单获取一级菜单
        for (Menu menu : menus) {
            if(menu.getParentMenuId()==null){
                Map map = new HashMap();
                map.put("path",menu.getPath());
                map.put("title",menu.getName());
                map.put("icon",menu.getIcon());
                map.put("children",getChildern(menus,menu.getId()));
                result.add(map);
            }
        }

        return result;
    }

    private List<Map> getChildern(List<Menu> menus, Integer id) {

        List<Map> list = new ArrayList<>();

        for (Menu menu : menus) {
            if(menu.getParentMenuId()==id){
                Map map = new HashMap();
                map.put("path",menu.getPath());
                map.put("title",menu.getName());
                map.put("linkUrl",menu.getLinkUrl());
                list.add(map);
            }
        }
        return list;
    }
}
